import { sl } from 'element-plus/es/locale'
import { ref, computed } from 'vue'
import {
  createInput,
  createSelect,
  createSlot,
  commonRules,
  createTextarea,
  createDate,
  createEmpty,
} from '@/components/DynamicForm/config'

const caseTypeList = [
  { value: '1', label: '简易案件' },
  { value: '2', label: '一般案件' },
  { value: '3', label: '重大案件' },
]

export const useTableColumns = (isFlow: boolean, disabled: boolean) => {
  const columns = ref({
    partyName: { label: '当事人名称', visible: true },
    caseCause: { label: '案由', visible: true },
    caseSource: { label: '案件来源', visible: true },
    caseNo: { label: '立案号', visible: true },
    caseProcedure: { label: '立案程序', visible: true },
    createTime: { label: '登记时间', visible: true },
    applyStatus: { label: '审批状态', visible: true },
  })

  // 表格列配置
  const newColumns = computed(() => [
    {
      type: 'selection',
      width: 55,
      align: 'center',
    },
    {
      type: 'index',
      label: '序号',
      width: 60,
      align: 'center',
      slots: {
        default: 'index',
      },
    },
    ...(columns.value.partyName.visible
      ? [
          {
            label: '当事人名称',
            key: 'partyName',
            prop: 'partyName',
            align: 'center' as const,
            minWidth: '180px',
          },
        ]
      : []),
    ...(columns.value.caseCause.visible
      ? [
          {
            label: '案由',
            key: 'caseCause',
            prop: 'caseCause',
            align: 'center' as const,
            minWidth: '220px',
          },
        ]
      : []),
    ...(columns.value.caseSource.visible
      ? [
          {
            label: '案件来源',
            key: 'caseSource',
            prop: 'caseSource',
            align: 'center' as const,
            minWidth: '180px',
            cellRenderer: {
              type: 'dictField',
              dictKey: 'case_source',
            },
          },
        ]
      : []),
    ...(columns.value.caseNo.visible
      ? [
          {
            label: '立案号',
            key: 'caseNo',
            prop: 'caseNo',
            align: 'center' as const,
            minWidth: '220px',
          },
        ]
      : []),
    ...(columns.value.caseProcedure.visible
      ? [
          {
            label: '立案程序',
            key: 'caseProcedure',
            prop: 'caseProcedure',
            align: 'center' as const,
            minWidth: '150px',
            cellRenderer: {
              type: 'dictField',
              dictKey: 'caseTypeList',
            },
          },
        ]
      : []),
    ...(columns.value.createTime.visible
      ? [
          {
            label: '登记时间',
            key: 'createTime',
            prop: 'createTime',
            align: 'center' as const,
            width: '160px',
          },
        ]
      : []),
    ...(columns.value.applyStatus.visible
      ? [
          {
            label: '审批状态',
            key: 'applyStatus',
            prop: 'applyStatus',
            align: 'center' as const,
            minWidth: '120px',
            cellRenderer: {
              type: 'dictField',
              dictKey: 'work_status',
              showTag: true,
            },
          },
        ]
      : []),
    ...(columns.value.applyStatus.visible
      ? [
          {
            label: '立案状态',
            key: 'status',
            prop: 'status',
            align: 'center' as const,
            minWidth: '120px',
            slots: {
              default: 'caseStatus',
            },
          },
        ]
      : []),
    {
      label: '操作',
      key: 'action',
      prop: 'action',
      align: 'center' as const,
      width: 240,
      fixed: 'right' as const,
      showOverflowTooltip: false,
      slots: {
        default: 'action',
      },
    },
  ])

  // 新增页表单
  const formConfigDetail = {
    labelWidth: '140px',
    gutter: 20,
    defaultSpan: 8,
    keyup: true,
    buttonAlign: 'center',
    isSearch: true,
    buttons: [],
  }

  const rulesDetail = {
    caseSource: [{ required: true, message: '案件来源不能为空', trigger: ['blur', 'change'] }],
  }

  const formItemsDetail = [
    createSelect('案件来源', 'caseSource', {
      placeholder: '请选择案件来源',
      dictKey: 'case_source',
      props: {
        style: { width: '100%' },
      },
      rules: [commonRules.required('案件来源不能为空')],
    }),

    // createSelect('测试级联', 'caseSource1', {
    //   placeholder: '请选择测试级联',
    //   optionKey: 'option222',
    //   props: {
    //     style: { width: '100%' },
    //   },
    // }),
    createSelect('立案程序', 'caseProcedure', {
      placeholder: '请选择立案程序',
      style: { width: '100%' },
      options: caseTypeList.filter(item => item.value !== '3'),
      props: {
        clearable: true,
      },
      rules: [commonRules.required('立案程序不能为空')],
    }),
  ]

  const formItemsDetailTwo = [
    createSlot('当事人名称', 'partyId', {
      slotName: 'partyId',
      props: {
        formTitle: '当事人信息',
      },
      rules: [commonRules.required('当事人名称不能为空')],
    }),
    createInput('法定代表人', 'legalRepresentative', {
      placeholder: '请输入法定代表人',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('法定代表人不能为空')],
    }),
    createInput('职务', 'position', {
      placeholder: '请输入职务',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('职务不能为空')],
    }),
    createInput('联系电话', 'contactPhone', {
      placeholder: '请输入联系号码',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('联系号码不能为空'), commonRules.phone],
    }),
    createInput('统一社会信用代码', 'unifiedSocialCreditCode', {
      placeholder: '请输入统一社会信用代码',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('统一社会信用代码不能为空')],
    }),
    createInput('营业执照类型', 'businessType', {
      placeholder: '请输入营业执照类型',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('营业执照类型不能为空')],
    }),
    createInput('邮政编码', 'postalCode', {
      placeholder: '请输入邮政编码',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('邮政编码不能为空')],
    }),
    createSlot('地址', 'streetOfCompanyAddress', {
      slotName: 'streetOfCompanyAddress',
      span: 12,
      rules: [commonRules.required('地址不能为空')],
    }),
    createSlot('', 'address', {
      slotName: 'address',
      span: 4,
      rules: [commonRules.required('详细地址不能为空')],
    }),

    createSlot('案由', 'caseCause', {
      slotName: 'caseCause',
      span: 24,
      props: {
        formTitle: '案件信息',
      },
      rules: [commonRules.required('案由不能为空')],
    }),
    createSlot('企业情况', 'companyProfile', {
      slotName: 'companyProfile',
      span: 24,
      props: {
        formTitleTwo: '案情简介及立案理由',
      },
      rules: [commonRules.required('企业情况不能为空')],
    }),
    createSlot('检查情况', 'siteDescription', {
      slotName: 'siteDescription',
      span: 24,
      rules: [commonRules.required('现场情况不能为空')],
    }),
    createSlot('违反条例', 'caseIntroduction', {
      slotName: 'caseIntroduction',
      span: 24,
      rules: [commonRules.required('违反条例不能为空')],
    }),
    createSlot('承办人意见', 'handlerOpinion', {
      slotName: 'handlerOpinion',
      span: 24,
      rules: [commonRules.required('承办人意见不能为空')],
    }),
    createSlot('备注', 'remarks', {
      slotName: 'remarks',
      span: 24,
    }),
    // createInput('执法人员', 'inspectorName1', {
    //   placeholder: '请输入执法人员',
    //   props: {
    //     clearable: true,
    //   },
    //   rules: [commonRules.required('执法人员不能为空')],
    // }),
    createSlot('执法人员', 'inspectorId1', {
      slotName: 'inspectorId1',
      placeholder: '请输入执法人员',
      rules: [commonRules.required('执法人员不能为空')],
    }),
    createInput('执法编号', 'inspectorCertNo1', {
      placeholder: '请输入执法编号',
      props: {
        clearable: true,
        disabled: disabled || isFlow,
      },
      // rules: [commonRules.required('执法编号不能为空')],
    }),
    createEmpty({ span: 8 }),
    createSlot('执法人员', 'inspectorId2', {
      slotName: 'inspectorId2',
      placeholder: '请输入执法人员',
      rules: [commonRules.required('执法人员不能为空')],
    }),
    createInput('执法编号', 'inspectorCertNo2', {
      placeholder: '请输入执法编号',
      props: {
        clearable: true,
        disabled: disabled || isFlow,
      },
      rules: [commonRules.required('执法编号不能为空')],
    }),
  ]

  const columnsDetail = [
    { type: 'index', label: '序号', width: 60, align: 'center' },
    {
      label: '检查时间',
      key: 'checkDate',
      prop: 'checkDate',
      align: 'center' as const,
      minWidth: 240,
      slots: {
        default: 'checkDate',
      },
    },
    {
      label: '检查地点',
      key: 'location',
      prop: 'location',
      align: 'center' as const,
      minWidth: 220,
      slots: {
        default: 'location',
      },
    },
    {
      label: '被检查单位名称',
      key: 'companyName',
      prop: 'companyName',
      align: 'center' as const,
      minWidth: 180,
    },
    {
      label: '现场负责人姓名',
      key: 'sitePrincipalName',
      prop: 'sitePrincipalName',
      align: 'center' as const,
      minWidth: 160,
    },
    {
      label: '操作',
      key: 'action',
      prop: 'action',
      align: 'center' as const,
      width: 200,
      fixed: 'right' as const,
      slots: {
        default: 'action',
      },
    },
  ]

  const formConfigCheck = {
    labelWidth: '120px',
    gutter: 20,
    defaultSpan: 8,
    keyup: true,
    buttonAlign: 'center',
    buttons: [],
  }

  const formItemsCheck = [
    createSlot('检查时间', 'checkDate', {
      slotName: 'checkDate',
      span: 10,
      style: { width: '100%' },
      rules: [commonRules.onlyDay],
    }),
    createSlot('地点', 'streetOfLocation', {
      slotName: 'streetOfLocation',
      span: 10,
      rules: [commonRules.required('地点不能为空')],
    }),
    createSlot('', 'location', {
      slotName: 'location',
      span: 4,
      formItemProps: {
        labelWidth: '0px',
      },
      rules: [commonRules.required('详细地点不能为空')],
    }),
    // createSelect('检查人', 'inspectorId1', {
    //   placeholder: '请选择检查人',
    //   dictKey: 'check_person',
    //   props: {
    //     style: { width: '100%' },
    //   },
    //   rules: [commonRules.required('检查人不能为空')],
    // }),
    createSlot('检查人', 'inspectorId1', {
      slotName: 'inspectorId1',
      rules: [commonRules.required('检查人不能为空')],
    }),
    createInput('执法证编号', 'inspectorCertNo1', {
      placeholder: '请输入执法证编号',
      props: {
        clearable: true,
      },
      rules: [commonRules.required('执法证编号不能为空')],
    }),
    // createSelect('检查人', 'inspectorId2', {
    //   placeholder: '请选择检查人',
    //   dictKey: 'check_person',
    //   props: {
    //     style: { width: '100%' },
    //   },
    //   rules: [commonRules.required('检查人不能为空')],
    // }),
    createEmpty({ span: 8 }),
    createSlot('检查人', 'inspectorId2', {
      slotName: 'inspectorId2',
      rules: [commonRules.required('检查人不能为空')],
    }),
    createInput('执法证编号', 'inspectorCertNo2', {
      placeholder: '请输入执法证编号',
      props: {
        clearable: true,
      },
      rules: [commonRules.required('执法证编号不能为空')],
    }),
    createEmpty({ span: 8 }),
    createSlot('记录人', 'recorderId', {
      slotName: 'recorderId',
      rules: [commonRules.required('记录人不能为空')],
    }),
    createInput('执法证编号', 'recorderCertNo', {
      placeholder: '请输入执法证编号',
      props: {
        clearable: true,
      },
      rules: [commonRules.required('执法证编号不能为空')],
    }),
    createInput('工作单位', 'workUnit', {
      placeholder: '请输入工作单位',
      props: {
        clearable: true,
      },
      rules: [commonRules.required('工作单位不能为空')],
    }),
    createSlot('被检查单位名称', 'partyId', {
      slotName: 'partyId',
      rules: [commonRules.required('被检查单位名称不能为空')],
    }),
    createInput('法定代表人', 'legalRepresentative', {
      placeholder: '请输入法定代表人',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('法定代表人不能为空')],
    }),
    createEmpty({ span: 8 }),
    createInput('现场负责人姓名', 'sitePrincipalName', {
      placeholder: '请输入现场负责人',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('现场负责人不能为空')],
    }),
    createDate('出生日期', 'sitePrincipalBirth', {
      type: 'date',
      placeholder: '请选择出生日期',
      props: {
        clearable: true,
        style: { width: '100%' },
        disabled: true,
      },
      rules: [commonRules.required('出生日期不能为空')],
    }),
    createSelect('性别', 'sitePrincipalGender', {
      placeholder: '请选择性别',
      dictKey: 'sys_user_sex',
      props: {
        style: { width: '100%' },
        disabled: true,
      },
      rules: [commonRules.required('性别不能为空')],
    }),
    createInput('公民身份号码', 'sitePrincipalIdNo', {
      placeholder: '请输入身份号码',
      span: 8,
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('身份号码不能为空'), commonRules.identityCard],
    }),
    createInput('电话', 'sitePrincipalPhone', {
      placeholder: '请输入电话',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('电话不能为空'), commonRules.phone],
    }),
    createInput('邮编', 'postalCode', {
      placeholder: '请输入邮编',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('邮编不能为空')],
    }),
    createInput('工作单位', 'sitePrincipalWorkUnit', {
      placeholder: '请输入工作单位',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('工作单位不能为空')],
    }),
    createInput('职务', 'sitePrincipalJob', {
      placeholder: '请输入职务',
      props: {
        clearable: true,
        disabled: true,
      },
      rules: [commonRules.required('职务不能为空')],
    }),
    createSlot('地址', 'companyAddress', {
      slotName: 'companyAddress',
      span: 10,
      props: {
        disabled: true,
      },
      rules: [commonRules.required('地址不能为空')],
    }),
    createSlot('', 'streetOfCompanyAddress', {
      slotName: 'streetOfCompanyAddress',
      span: 6,
      formItemProps: {
        labelWidth: '0px',
      },
      props: {
        disabled: true,
      },
      rules: [commonRules.required('详细地址不能为空')],
    }),

    createSlot('企业情况', 'companyProfile', {
      slotName: 'companyProfile',
      span: 24,
      props: {
        formTitleTwo: '现场情况',
        left: '45px',
      },
      rules: [commonRules.required('企业情况不能为空')],
    }),
    createSlot('检查情况', 'siteDescription', {
      slotName: 'siteDescription',
      span: 24,
      rules: [commonRules.required('现场情况不能为空')],
    }),
    createSlot('现场留证情况', 'siteRetention', {
      slotName: 'siteRetention',
      span: 24,
      rules: [commonRules.required('现场留证情况不能为空')],
    }),
  ]

  return {
    columns,
    newColumns,
    rulesDetail,
    formConfigDetail,
    formItemsDetail,
    formItemsDetailTwo,
    columnsDetail,
    formConfigCheck,
    formItemsCheck,
  }
}
